package com.biao.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biao.common.utils.PageUtils;
import com.biao.common.utils.Query;
import com.biao.mall.coupon.dao.SeckillSessionDao;
import com.biao.mall.coupon.entity.SeckillSessionEntity;
import com.biao.mall.coupon.entity.SeckillSkuRelationEntity;
import com.biao.mall.coupon.service.SeckillSessionService;
import com.biao.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {

        LambdaQueryWrapper<SeckillSessionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SeckillSessionEntity::getStartTime, dateCalculation()[0], dateCalculation()[1]);
        List<SeckillSessionEntity> list = list(wrapper);
        if(list!=null&&list.size()>0){
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                Long id = session.getId();
                //查询并设置每场活动关联的秒杀商品
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                session.setRelationSkus(relationEntities);
                return session;
            }).collect(Collectors.toList());
            return collect;
        }

//        List<SeckillSessionEntity> entityList= baseMapper.selectThreeDayEntity();
        return null;

    }

    private String[] dateCalculation() {
        LocalDate now = LocalDate.now();
        LocalDate days = now.plusDays(3);
        LocalTime max = LocalTime.MAX;
        LocalTime min = LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);
        LocalDateTime of1 = LocalDateTime.of(days, max);
        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm:ss"));//当天 00:00
        String format1 = of1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm:ss"));//三天后23:59
        return new String[]{format, format1};
    }
}